package com.tg.app.helper;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.module.appcommon.BuildConfig;
import com.module.appcommon.R;
import com.tg.app.view.MsgCenterToast;
import com.tg.appcommon.android.TGAlertDialog;

import com.tg.appcommon.android.TGLog;

import java.lang.reflect.Method;


public class PermissionHelper {

    private static final String TAG = "PermissionHelperTAG";

    public static void requestFloatingWindowPermission(Context context) {
        if (context == null) return;
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        context.startActivity(intent);
    }

    @SuppressLint("WrongConstant")
    private static boolean isAndroidFloatingPermissionGranted(Context context) {
        try {
            AppOpsManager manager = (AppOpsManager) context.getSystemService("appops");
            // 24表示悬浮窗权限在AppOpsManager中AppOpsManager.OP_SYSTEM_ALERT_WINDOW,但是无法直接使用
            Object object = invokeMethod(manager, "checkOp", 24, Binder.getCallingUid(), context.getPackageName());
            TGLog.i(TAG, "[isFloatingPermissionGranted] AppOpsManager.MODE_ALLOWED = " + AppOpsManager.MODE_ALLOWED);
            TGLog.i(TAG, "[isFloatingPermissionGranted] object = " + object);
            return AppOpsManager.MODE_ALLOWED == (Integer) object;
        } catch (Throwable e) {
            TGLog.i(TAG, "[isFloatingPermissionGranted] " + Log.getStackTraceString(e));
        }
        return false;
    }

    @SuppressWarnings("rawtypes")
    public static Object invokeMethod(AppOpsManager manager, String method, int op, int uid, String packageName) {
        Class<? extends AppOpsManager> c = manager.getClass();
        try {
            Class[] classes = new Class[]{int.class, int.class, String.class};
            Object[] x2 = {op, uid, packageName};
            Method m = c.getDeclaredMethod(method, classes);
            return m.invoke(manager, x2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }


    public static boolean isFloatingWindowsPermissionGranted(Context context) {
        if (context == null) return false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return isAndroidFloatingPermissionGranted(context);
        } else {
            // Android 6.0 之前，默认有悬浮窗权限
            return true;
        }
    }

    public static  void requestStoragePermission(Context context, int resID){
        new TGAlertDialog(context).builder().setMessage(resID).setPositiveButton(R.string.go_to_settings, new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                XXPermissions.with(context).permission(Permission.MANAGE_EXTERNAL_STORAGE).request((permissions, all) -> {

                });

            }
        }).setNegativeButton(R.string.cancel, v -> {

        }).show();
    }

}
